package com.neusoft.elmboot.service.plus.impl;

import com.neusoft.elmboot.mapper.plus.VirtualWalletMapper;
import com.neusoft.elmboot.po.domain_plus.dto.WalletFlowDto;
import com.neusoft.elmboot.po.domain_plus.vo.WalletFlowVo;
import com.neusoft.elmboot.service.plus.VirtualWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class VirtualWalletServiceImpl implements VirtualWalletService {
    @Autowired
    private VirtualWalletMapper virtualWalletMapper;

    @Override
    public int addWalletById(String userId) {
        return virtualWalletMapper.addWalletById(userId);
    }

    @Override
    public int getWhetherEnabledById(String userId) {
        return virtualWalletMapper.getWhetherEnabledById(userId);
    }

    @Override
    @Transactional
    public int rechargeById(WalletFlowDto walletFlowDto) {
        virtualWalletMapper.addRechargeFlow(walletFlowDto);
        return virtualWalletMapper.rechargeById(walletFlowDto);
    }

    @Override
    @Transactional
    public int withdrawById(WalletFlowDto walletFlowDto) {
        if(virtualWalletMapper.getAmountById(walletFlowDto).compareTo(walletFlowDto.getAmount()) < 0){
            return 0;
        }
        virtualWalletMapper.addWithdrawFlow(walletFlowDto);
        virtualWalletMapper.withdrawById(walletFlowDto);
        return 1;
    }

    @Override
    @Transactional
    public int pay(WalletFlowDto walletFlowDto) {
        //转账人钱不够
        if(virtualWalletMapper.getAmountById(walletFlowDto).compareTo(walletFlowDto.getAmount()) < 0){
            return 0;
        }
        //不能转账给自己
        if(walletFlowDto.getUserId()==walletFlowDto.getTargerId()){
            return 0;
        }
        //支付对方不存在的问题
        if(virtualWalletMapper.getCountById(walletFlowDto.getTargerId()) ==0 ){
            return 0;
        }
        //这里如果发生异常，则事务回滚+在controller层中return 0
        virtualWalletMapper.addPayFlow(walletFlowDto);
        virtualWalletMapper.withdrawById(walletFlowDto);
        WalletFlowDto walletFlowDto1 = new WalletFlowDto();
        walletFlowDto1.setAmount(walletFlowDto.getAmount());
        walletFlowDto1.setUserId(walletFlowDto.getTargerId());
        virtualWalletMapper.rechargeById(walletFlowDto1);
        return 1;
    }

    @Override
    public BigDecimal getBalanceById(String userId) {
        WalletFlowDto walletFlowDto = new WalletFlowDto();
        walletFlowDto.setUserId(userId);
        return virtualWalletMapper.getAmountById(walletFlowDto);
    }

    @Override
    public List<WalletFlowVo> listWalletLogById(String userId) {
        return virtualWalletMapper.listWalletLogById(userId);
    }

    @Override
    public List<WalletFlowVo> listWalletLogIncomeById(String userId) {
        return virtualWalletMapper.listWalletLogIncomeById(userId);
    }

    @Override
    public List<WalletFlowVo> listWalletLogOutcomeById(String userId) {
        return virtualWalletMapper.listWalletLogOutcomeById(userId);
    }

    @Override
    public int getFreeze(String userId) {
        return virtualWalletMapper.getFreeze(userId);
    }

    @Override
    public int freeze(String userId) {
        return virtualWalletMapper.freeze(userId);
    }

    @Override
    public int unFreeze(String userId) {
        return virtualWalletMapper.unFreeze(userId);
    }

}
